package com.gitee.feizns.quickstart.validation.validator;

import com.gitee.feizns.quickstart.validation.constraints.Phone;

import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * 手机格式验证器
 * @author feizns
 * @since 2023/2/5
 */
public class PhoneConstraintValidator extends AbstractConstraintValidator<Phone, String> {

    /**
     * 手机号验证正则
     */
    private static final Pattern phonePattern = Pattern.compile("^((13\\d)|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18\\d)|(19[0,1,5,6,7,8,9]))\\d{8}$");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return value == null || phonePattern.matcher(value).matches();
    }

}
